#include "BancoClass.h"

BancoClass::BancoClass()
{
}
bool BancoClass::AutenticarPalavraPasse(QString palavra)
{

    QSqlDatabase db = QSqlDatabase::addDatabase( "QSQLITE" );
    db.setDatabaseName( "./database.db" );
    db.open();
    qDebug() << db.lastError();

    QSqlQuery query("SELECT * FROM usuario WHERE palavra_passe='"+palavra+"'");
    qDebug() << query.lastError();

    QString valor;

    while (query.next())
    {
        QString cont = query.value(0).toString();
        valor = cont;
    }

    if(valor == "")
    {
        return false;
    }
    else
    {
        return true;
    }

    db.close();

}

bool BancoClass::AutenticarUsuario(QString usuario, QString senha)
{

    QSqlDatabase db = QSqlDatabase::addDatabase( "QSQLITE" );
    db.setDatabaseName( "./database.db" );
    db.open();
    qDebug() << db.lastError();

    QSqlQuery query("SELECT * FROM usuario WHERE nome='"+usuario+"' AND senha='"+senha+"'");
    qDebug() << query.lastError();

    QString valor;

    while (query.next())
    {
        QString cont = query.value(0).toString();
        valor = cont;
    }

    if(valor == "")
    {
        return false;
    }
    else
    {
        return true;
    }

    db.close();

}

bool BancoClass::cadastrarUsuario(QString usuario, QString senha, QString palavra)
{
    QSqlDatabase db = QSqlDatabase::addDatabase( "QSQLITE" );
    db.setDatabaseName( "./database.db" );
    db.open();

    QSqlQuery query;
    query.prepare("INSERT INTO usuario (nome, senha, palavra_passe)"
                  "VALUES (:nome, :senha, :palavra)");
    query.bindValue(":nome", usuario);
    query.bindValue(":senha", senha);
    query.bindValue(":palavra", palavra);
    query.exec();

    qDebug() << query.lastError();
    db.close();

    return true;
}

QSqlQuery BancoClass::getUsuarioByNome(QString usuario) {

    QSqlDatabase db = QSqlDatabase::addDatabase( "QSQLITE" );
    db.setDatabaseName( "./database.db" );
    db.open();
    qDebug() << db.lastError();

    QSqlQuery query("SELECT * FROM usuario WHERE nome='"+usuario+"'");
    qDebug() << query.lastError();

    return query;

    db.close();

}

QSqlQuery BancoClass::getUsuarioByPalavraPasse(QString palavra) {

    QSqlDatabase db = QSqlDatabase::addDatabase( "QSQLITE" );
    db.setDatabaseName( "./database.db" );
    db.open();
    qDebug() << db.lastError();

    QSqlQuery query("SELECT * FROM usuario WHERE palavra_passe='"+palavra+"'");
    qDebug() << query.lastError();

    return query;

    db.close();

}
